import {orderList, changeOrderInfo, orderDetail, orderSendGoods, expressCompany, exportOrder, statusCount} from '../services/api';
import {message} from 'antd';

export default {
    namespace: 'orderList',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        orderDetail: {},
        statusCount: {
            '1' : 0, //全部
            '102': 0,
            '100': 0,
            '103': 0,
            '200': 0,
            '105': 0,
            '104': 0,
        },
        loading: false,
    },

    effects: {
        *fetch({payload}, {call, put}) {
            const response = yield call(orderList, payload);
            if (response) {
                yield put({
                    type: 'save',
                    payload: {data: response},
                });
            }
        },
        *getStatusCount({payload}, {call, put}) {
            const response = yield call(statusCount, payload);
            if (response) {
                yield put({
                    type: 'saveStatusCount',
                    payload: response,
                });
            }
        },
        *changeInfo({payload}, {call, put}) {
            const response = yield call(changeOrderInfo, payload);
            if (response) {
                message.success('成功');
                if (payload.status) {
                    //修改状态值
                    yield put({
                        type: 'saveChangeInfo',
                        payload: payload,
                    });
                }

            } else {
                message.error('失败');
            }
        },
        *getOrderDetail({payload}, {call, put}) {
            const response = yield call(orderDetail, payload);
            if (response) {
                yield put({
                    type: 'save',
                    payload: {orderDetail: response},
                });
            }
        },
        *getExpressCompany({payload}, {call, put}) {
            const response = yield call(expressCompany, payload);
            if (response) {
                //yield response;
                if (typeof payload.callback == 'function') {
                    payload.callback(response);
                }
                // yield put({
                //   type: 'save',
                //   payload: {expressCompany : response},
                // });
            }
        },
        *sendGoods({payload}, {call, put}) {
            const response = yield call(orderSendGoods, payload);
            if (response) {
                yield put({
                    type: 'saveSendGoods',
                    payload: payload,
                });
                message.success('成功');
            } else {
                message.error('失败');
            }
        },
        *exportTable({payload}, {call, put}) {
            const response = yield call(orderSendGoods, payload);
            if (response) {

                message.success('成功');
                // yield put({
                //   type: 'saveSendGoods',
                //   payload: payload,
                // });
            } else {
                message.error('失败');
            }
        },
    },

    reducers: {
        save(state, {payload}){
            return {
                ...state,
                ...payload
            }
        },
        saveChangeInfo(state, {payload}){
            const {orderId, status} = payload;
            let temp = {...state.data};

            temp.list.forEach(v=> {
                if (v.orderId == orderId) {
                    v.status = status
                }
            });

            return {
                ...state,
                data: data,
            }
        },
        saveSendGoods(state, {payload}){
            const {orderId} = payload;
            let temp = {...state.data};

            temp.list.forEach(v=> {
                if (v.orderId == orderId) {   //修改当前记录为发货状态
                    v.status = 103
                }
            });

            return {
                ...state,
                data: temp,
            }
        },
        saveStatusCount(state, {payload}){
            let total = 0;
            let statusCount = {
                '1' : 0, //全部
                '102': 0,
                '100': 0,
                '103': 0,
                '200': 0,
                '105': 0,
                '104': 0
            };
            payload.forEach(v=>{
                statusCount[v.status] = v.count;
                total += v.count;  //全部数量用每个状态相加
            });
            statusCount['1'] = total; 
            return {
                ...state,
                ...{statusCount}
            }
        },
    },
};
